package com.example.cartmanage.chewei.view

import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.cartmanage.databinding.ActivityAddCheweiBinding
import com.blankj.utilcode.util.ToastUtils
import com.example.cartmanage.chewei.model.CheweiViewModel
import com.example.lib_base.mvvm.BaseActivity
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/cartmanage/AddCheweiActivity")
class AddCheweiActivity :  BaseActivity<ActivityAddCheweiBinding, CheweiViewModel>() {
    private var status: String = "空闲"

    override fun initView() {
        binding.back.setOnClickListener { finish() }
        // 区域和类型下拉
        val areaList = listOf("A区地下停车场", "B区地面停车场")
        val typeList = listOf("标准车位", "充电车位", "无障碍车位")
        binding.spArea.adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, areaList)
        binding.spType.adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, typeList)

        // 车位状态选择
        val statusBtns = listOf(binding.btnStatusFree, binding.btnStatusUsed, binding.btnStatusMaintain)
        statusBtns.forEach { btn ->
            btn.setOnClickListener {
                statusBtns.forEach { it.isSelected = false }
                btn.isSelected = true
                status = btn.text.toString()
            }
        }
        binding.btnStatusFree.isSelected = true

        binding.btnSave.setOnClickListener {
            val spaceNumber = binding.etSpaceNumber.text.toString()
            val area = binding.spArea.selectedItem.toString()
            val type = binding.spType.selectedItem.toString()
            val userName = binding.etUserName.text.toString()
            val licensePlate = binding.etLicensePlate.text.toString()
            val remarks = binding.etRemarks.text.toString()
            val map = linkedMapOf<String, Any>()
            // 按照时间顺序排序字段
            map["areaId"] = binding.spArea.selectedItemPosition + 1 // 如有实际ID请替换
            map["areaName"] = area
            map["spaceNumber"] = spaceNumber
            map["spaceType"] = type
            map["spaceStatus"] = status
            map["userName"] = userName
            map["licensePlate"] = licensePlate
            map["remarks"] = remarks
            vm.addChewei(map)
        }
    }

    override fun initData() {
        vm.addCheweiSuccess.observe(this) {
            ToastUtils.showShort("添加成功")
            finish()
        }
    }


}